<?php
namespace app\admin\controller;
use think\Controller;


Class Article extends BaseAdmin
{

	//文章列表
    public function lst()
    {
    	
        
        $data=db('tp_article')->paginate(10);
        $cate=db('tp_cate')->select();

        $this->assign(
            [
                'data'=>$data,
                'cate'=>$cate,


            ]
        );
        return $this->fetch('list');
    }

    //文章添加
    public function add()
    {

        
        $obj=db('tp_cate');
        $data=$obj->order('sort DESC')->select();
        $data=$this->CateTree->catetree($data);
        $this->assign('data',$data);
    	 


        if(request()->isPost()){
        	$data=input('post.');
        	if($data['link_url']&&stripos($data['link_url'],'http://')===false){
        		$data['link_url']='http://'.$data['link_url'];
        	}

        	if($_FILES['thumb']['tmp_name']){
        		$data['thumb']=$this->upload();
        	}


        	//验证
        	$validate = new \app\admin\validate\Article;
        	if (!$validate->check($data)) {
        		$this->error($validate->getError());
        	}
            $data['add_time']=time();
        	
        	$add=db('tp_article')->insert($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }




    //文章编辑
    public function edit($id)
    {
    	
        $obj=db('tp_cate');
        $cate=$obj->order('sort DESC')->select();
        $cate=$this->CateTree->catetree($cate);

        $data=db('tp_article')->where(array('id'=>$id))->find();
        $this->assign(
            [
                'data'=>$data,
                'cate'=>$cate,
            ]
        );


    	if(request()->isPost()){
        	$datas=input('post.');
        	if($datas['link_url']&&stripos($datas['link_url'],'http://')===false){
        		$datas['link_url']='http://'.$datas['link_url'];
        	}

        	if($_FILES['thumb']['tmp_name']){
        		$datas['thumb']=$this->upload();
        	}
        	$up=db('tp_article')->where(array('id'=>$id))->update($datas);
        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }




    //文章删除
    public function del($id)
    {
        //判断是否存在缩略图，存在就删除
        $tp=db('tp_article');
    	$img=$tp->field('thumb')->find($id);
        $del=$tp->delete($id);
        $img=IMG_UPLOADS.$img['thumb'];
        if(file_exists($img)){
            @unlink($img);
        }
        if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }

    
    //图片上传
	public function upload(){
		$file=request()->file('thumb');
		if($file==null){
			exit(json_encode(array('msg'=>'没有文件上传')));
		}
		$info=$file->move('../public/static/uploads');
		$ext=$info->getExtension();//获取后缀名
		if(!in_array($ext,array('jpg','png','gif','jpeg'))){
			exit(json_encode(array('msg'=>'文件格式不符合')));
		}
		if($info){
			return $img=$info->getSaveName();

		}else{
			$file->getError();
			die;
		}
		
		
	}


    //Ueditor图片列表
    public function ImgList(){
        $_files=my_scandir();
        $files=array();
        foreach ($_files as $k => $v) {
            if(is_array($v)){
                foreach ($v as $k1 => $v1) {
                    $v1=str_replace(UEDITOR,HTTP_UEDITOR,$v1);
                    $files[]=$v1;
                }
            }else{
                $v=str_replace(UEDITOR,HTTP_UEDITOR,$v);
                $files[]=$v;
            }
        }

        $this->assign('files',$files);
        // dump($files);
        // exit;
        return $this->fetch();
    }


    public function delimg(){
        $imgSrc=input('post.imgsrc');
        $imgSrc=DEL_UEDITOR.$imgSrc;
        // dump($imgSrc);
        // exit;

        if(file_exists($imgSrc)){
            if(unlink($imgSrc)){
                echo 1;
            }else{
                echo 2;
            }
        }else{
            echo 3;
        }


        

        
    }


}